// CREATE TABLE `t_post` (
//     `id` int(11) NOT NULL AUTO_INCREMENT,
//     `pid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
//     `uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '发帖人',
//     `tid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '话题id',
//     `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
//     `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
//     `imgs` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
//     `create_date` datetime DEFAULT NULL,
//     `update_date` datetime DEFAULT NULL,
//     PRIMARY KEY (`id`,`pid`)
//   ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

let { v4: uuidv4 } = require("uuid")
const knex = require("../dao")
let TNAME = "t_post";
class Post {
    Add = (data, callback) => {
        data.pid = uuidv4();
        knex(TNAME).insert(data).then(res => {
            callback(undefined, { code: 0, msg: "添加成功", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Update = (data, callback) => {
        let { pid } = data
        knex(TNAME).where({ pid }).update(data).then(res => {
            callback(undefined, { code: 0, msg: "更新成功", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    List = ({ page, page_size }, callback) => {
        knex(TNAME).select().offset(page_size || 10).limit(page || 0).then(res => {
            callback(undefined, { code: 0, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Detail = ({ pid }, callback) => {
        knex(TNAME).select().where({ pid }).then(res => {
            callback(undefined, { code: 0, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Remove = ({ pid }, callback) => {
        knex(TNAME).where({ pid }).del().then(res => {
            callback(undefined, { code: 0, msg: "删除成功", data: res })

        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: res })

        })
    }

}

module.exports = Post